<template>
  <main class="site-content" :class="{ 'site-content--tabs': $route.meta.isTab }">
    <!-- 主入口标签页 s -->
    <el-tabs
      v-if="$route.meta.isTab"
      v-model="mainTabsActiveName"
      @tab-click="selectedTabHandle"
      @tab-remove="removeTabHandle">
      <el-dropdown class="site-tabs__tools" :show-timeout="0">
        <div>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="tabsCloseCurrentHandle">关闭当前标签页</el-dropdown-item>
          <el-dropdown-item @click.native="tabsCloseOtherHandle">关闭其它标签页</el-dropdown-item>
          <el-dropdown-item @click.native="tabsCloseAllHandle">关闭全部标签页</el-dropdown-item>
          <el-dropdown-item @click.native="tabsRefreshCurrentHandle">刷新当前标签页</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-tab-pane
        v-for="item in mainTabs"
        :key="item.name"
        :closable="item.name != 'home'"
        :label="item.title"
        :name="item.name">
      </el-tab-pane>
      <el-card :body-style="siteContentViewHeight">
        <transition name="fade-transform" mode="out-in">
          <keep-alive :include="keepAliveRoutes">
            <router-view :key="menuActiveName" v-if="!isReload"/>
          </keep-alive>
          <!--<keep-alive v-if="keepAliveList.includes(item.name)">
           <router-view v-if="item.name === mainTabsActiveName"/>
         </keep-alive>
         <template v-else>
           <router-view v-if="item.name === mainTabsActiveName"/>
         </template>-->
        </transition>
      </el-card>
    </el-tabs>
    <!-- 主入口标签页 e -->
    <el-card v-else :body-style="siteContentViewHeight">
      <transition
        name="fade-transform"
        mode="out-in">
        <router-view/>
      </transition>
    </el-card>
  </main>
</template>

<script>
  import {isURL} from '@/utils/validate'
  import {mapGetters} from 'vuex'
  export default {
    components: {

    },
    data () {
      return {
        isReload: false
      }
    },
    computed: {
      ...mapGetters(['curRouteName', 'keepAliveRoutes']),
      documentClientHeight: {
        get () {
          return this.$store.state.common.documentClientHeight
        }
      },
      menuActiveName: {
        get () {
          return this.$store.state.common.menuActiveName
        },
        set (val) {
          this.$store.commit('common/updateMenuActiveName', val)
        }
      },
      mainTabs: {
        get () {
          return this.$store.state.common.mainTabs
        },
        set (val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      },
      mainTabsActiveName: {
        get () {
          return this.$store.state.common.mainTabsActiveName
        },
        set (val) {
          this.$store.commit('common/updateMainTabsActiveName', val)
        }
      },
      siteContentViewHeight () {
        let height = this.documentClientHeight - 50 - 30 - 3
        if (this.$route.meta.isTab) {
          height -= 40
          return isURL(this.$route.meta.iframeUrl) ? {height: height + 'px', padding: 0} : {minHeight: height + 'px'}
        }
        return {minHeight: height + 'px'}
      },
      keepAliveList () {
        return this.$store.state.keepAliveList
      }
    },
    methods: {
      // tabs, 选中tab
      selectedTabHandle (tab) {
        tab = this.mainTabs.filter(item => item.name === tab.name)
        if (tab.length >= 1) {
          this.$router.push({name: tab[0].name})
        }
      },
      // tabs, 删除tab
      removeTabHandle (tabName) {
        this.mainTabs = this.mainTabs.filter(item => item.name !== tabName)
        if (this.mainTabs.length >= 1) {
          // 当前选中tab被删除
          if (tabName === this.mainTabsActiveName) {
            this.$router.push({name: this.mainTabs[this.mainTabs.length - 1].name}, () => {
              this.mainTabsActiveName = this.$route.name
            })
          }
        } else {
          this.menuActiveName = ''
          this.$router.push({name: 'home'})
        }
        this.$store.commit('common/delKeepAlive', tabName)
      },
      // tabs, 关闭当前
      tabsCloseCurrentHandle () {
        if (this.mainTabsActiveName === 'home') return
        this.removeTabHandle(this.mainTabsActiveName)
      },
      // tabs, 关闭其它
      tabsCloseOtherHandle () {
        this.mainTabs = this.mainTabs.filter(item => item.name === this.mainTabsActiveName || item.name === 'home')
      },
      // tabs, 关闭全部
      tabsCloseAllHandle () {
        this.mainTabs = [
          {name: 'home', title: '首页'}
        ]
        this.menuActiveName = ''
        this.$router.push({name: 'home'})
      },
      // tabs, 刷新当前
      tabsRefreshCurrentHandle () {
        let tempTabName = this.mainTabsActiveName
        this.$store.commit('common/delKeepAlive', tempTabName)
        this.isReload = true
        this.$nextTick(() => {
          this.isReload = false
          this.$store.commit('common/addKeepAlive', tempTabName)
        })
      }
    }
  }
</script>

<style>
  .no_menu .site-wrapper {
    margin-top: 0;
  }
  .site-content__wrapper {
    height: calc(100% - 50px);
  }
  /*.site-sidebar {
    display: none;
  }
  .site-navbar {
    display: none;
  }
  .el-tabs__header {
    display: none;
  }
  .site-tabs__tools {
    display: none;
  }
  .site-content--tabs {
    padding: 0;
  }
  .site-content__wrapper {
    margin-left: 0;
    padding-top: 20px;
  }*/
</style>
